package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.rfid.project.exception.CannotDelException;
import com.mysema.query.BooleanBuilder;

import java.util.List;

@Service
@Transactional
public class ProjectTaskTypeService{

	@Autowired
	private ProjectTaskTypeDao projectTaskTypeDao;
	
	private ProjectTaskDao projectTaskDao;

	private QProjectTaskType $ = QProjectTaskType.projectTaskType;
	
	private QProjectTask $_ = QProjectTask.projectTask;
	
	@Autowired
	public void setProjectTaskTypeDao(ProjectTaskTypeDao projectTaskTypeDao) {
		this.projectTaskTypeDao = projectTaskTypeDao;
	}
	
	@Autowired
	public void setProjectTaskDao(ProjectTaskDao projectTaskDao) {
		this.projectTaskDao = projectTaskDao;
	}
	
	public List<ProjectTaskType> findAll(){
		return ListUtil.toList(projectTaskTypeDao.findAll());
	}
				
	public List<ProjectTaskType> findAll(
	String name,
	String remark,
	String orderByAttribute	){
	BooleanBuilder condition = new BooleanBuilder();
	
	if(StringUtil.isNotEmpty(name)){
		condition.and($.name.contains(name));
	}
	
	
	if(StringUtil.isNotEmpty(remark)){
		condition.and($.remark.contains(remark));
	}
	
	
	return ListUtil.toList(projectTaskTypeDao.findAll(condition));
	}		

	public Page<ProjectTaskType> findAll(
			String name,
			String remark,
			int pageNumber, int pageSize,String sortAttribute,String orderDirection) {
			BooleanBuilder condition = new BooleanBuilder();
	
			if(StringUtil.isNotEmpty(name)){
				condition.and($.name.contains(name));
			}
			if(StringUtil.isNotEmpty(remark)){
				condition.and($.remark.contains(remark));
			}
	
			/*
			Sort sortObject = null;
			if(StringUtil.isEmpty(sortAttribute)){
				sortObject = new Sort(Direction.DESC, "id");
			}else{
				sortObject = new Sort(Direction.fromStringOrNull(orderDirection),sortAttribute);
			}
			*/
			
			Sort sortObject_1 = new Sort(Direction.DESC, "orderNumber");
			
			PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sortObject_1);
			return projectTaskTypeDao.findAll(condition, pageRequest);
	}
	
	
	
	public ProjectTaskType getProjectTaskType(Long id){
		return projectTaskTypeDao.getOne(id);
	}
	
	public void deleteProjectTaskType(Long id) throws CannotDelException{
		if(this.findTaskByType(id)){
			throw new CannotDelException("项目任务类型已经被项目任务引用，请先项目任务信息或修改项目任务的引用！");
		}
		else{
			this.projectTaskTypeDao.delete(id);
		}
	}
	
	private boolean findTaskByType(Long typeId){
		BooleanBuilder condition = new BooleanBuilder();
		condition.and($_.projectTaskType.id.eq(typeId));
		return this.projectTaskDao.count(condition) > 0;
	}
	
	
	public void saveProjectTaskType(ProjectTaskType projectTaskType) {
		if (null == projectTaskType.getOrderNumber())
			projectTaskType.setOrderNumber(this.getOrderNumber());
		projectTaskTypeDao.save(projectTaskType);
	}
	
	private int getOrderNumber(){
		BooleanBuilder condition = new BooleanBuilder();
		Sort sortObject = new Sort(Direction.DESC, "orderNumber");
		PageRequest pageRequest = new PageRequest(0, 1, sortObject);
		Page<ProjectTaskType> projectTaskTypes = this.projectTaskTypeDao.findAll(condition,pageRequest);
		if(projectTaskTypes.getTotalElements() > 0){
			ProjectTaskType projectTaskType = projectTaskTypes.getContent().get(0);
			return projectTaskType.getOrderNumber()+1;
		}
		return 1;
	}
	
	public boolean changeOrderNumber(Long id, Long targetId){
		ProjectTaskType currentModule = this.projectTaskTypeDao.findOne(id);
		ProjectTaskType targetModule = this.projectTaskTypeDao.findOne(targetId);
		int orderNumber = currentModule.getOrderNumber();
		currentModule.setOrderNumber(targetModule.getOrderNumber());
		targetModule.setOrderNumber(orderNumber);
		
		this.projectTaskTypeDao.save(currentModule);
		this.projectTaskTypeDao.save(targetModule);
		return true;
	}

	public ProjectTaskType findOne(Long id) {
		return this.projectTaskTypeDao.findOne(id);
	}
	
	
}
		
